package ru.spbu.math.android.cbrf.model;

import java.util.Comparator;

/**
 * Created by IntelliJ IDEA. User: yan lobkarev mail: yan.lobkarev@gmail.com Date: 30.04.2010 Time: 12:48:33
 */
public class RateType {

    private String sourceCode;
    private String targetCode;

    public String getSourceCode() {
        return sourceCode;
    }

    public String getTargetCode() {
        return targetCode;
    }

    public RateType(String sourceCode, String targetCode) {
        this.sourceCode = sourceCode;
        this.targetCode = targetCode;
    }

  	@Override
    public boolean equals(Object o) {
//        boolean res = false;
//        try {
//            RateType rateType = (RateType) o;
//            if (this.sourceCode.equals(rateType.getSourceCode())
//                    && this.targetCode.equals(rateType.getTargetCode())) {
//                res = true;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            return res;
//        }
    	
    	if (o == null) {
    		return false;
    	}
    	
    	try {
			RateType rateType = (RateType) o;
			if (this.sourceCode.equals(rateType.getSourceCode())
					&& this.targetCode.equals(rateType.getTargetCode())) {
				return true;
			} else {
				return false;
			}
		} catch (ClassCastException e) {
			return false;
		}    	
    }

    @Override
    public int hashCode() {
    	return toString().hashCode();
    }

    @Override
    public String toString() {
        return sourceCode+"/"+targetCode;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return new RateType(sourceCode, targetCode);
    }
    
    public static Comparator<RateType> getComparator() {
    	return new RateTypeComparator();
    }
    
    private static class RateTypeComparator implements Comparator<RateType> {

		public int compare(RateType rateType1, RateType rateType2) {
			
			if (rateType1.getSourceCode().compareTo(rateType2.getSourceCode()) == 0) {
				return rateType1.getTargetCode().compareTo(rateType2.getTargetCode());
			} else {
				return rateType1.getSourceCode().compareTo(rateType2.getSourceCode());
			}
		}
    }
}
